<?php
require_once 'Consts.inc';
class DBConnecter extends Consts {
	private $mbMysql = null;
	function __destruct() {
		$this->closeDB ();
	}
	function connectDB() {
		if ($this->mbMysql == null) {
			$this->mbMysql = mysqli_connect ( self::CONST_HOST, self::CONST_USER, self::CONST_PASSWORD );
			if (mysqli_connect_errno ()) {
				echo "<script>alert(\"DB접속 에러입니다.\"); window.location.replace('".self::CONST_HOME."');</script>";
				exit ();
				return;
			}
		}
	}
	function closeDB() {
		if ($this->mbMysql != null) {
			mysqli_close ( $this->mbMysql );
		}
	}
	function selectDB($dbName) {
		if ($this->mbMysql != null) {
			$selected = mysqli_select_db ( $this->mbMysql, $dbName );
			if (! $selected) {
				echo "<script>alert(\"DB 테이블을 찾을 수 없습니다.\"); window.location.replace('".self::CONST_HOME."');</script>";
				$this->closeDB ();
				exit ();
				return;
			}
		}
	}
	function query($query) {
		if ($this->mbMysql != null) {
			$result = mysqli_query ( $this->mbMysql, $query );
			if (empty ( $result )) {
				echo "<script>alert(\"DB 쿼리를 실행할 수 없습니다.\"); window.location.replace('".self::CONST_HOME."');</script>";
				$this->closeDB ();
				exit ();
				return;
			}
			return $result;
		}
	}
	function free($result) {
		if ($this->mbMysql != null) {
			if (! empty ( $result )) {
				$result->free;
			}
		}
	}
	function getMySqlConneter() {
		return $this->mbMysql;
	}
}
?>